clear all
cd "data"

import excel using "Study 3 Open Ended Coding.xlsx", first clear
assert match == 1
rename Mostsurprising t1_open_end

tab Code, mis
disp (25.96 + 6.73 + 5.77 + 11.54) / (100-15.38)

drop if missing(t1_open)
replace t1_open = trim(t1_open)
tempfile open
save `open'

use partisan_canvasser_endline_baseline_clean.dta, replace

count

keep if t1_respondent == 1

count

replace t1_open = trim(t1_open)
merge m:1 t1_open_end using `open', assert(1 3)
assert _merge == 1 if missing(t1_open)
replace Code = "Skipped" if missing(t1_open)
rename Code t1_open_end_coding

// Create Demographic Table
label var t0_age Age
label var t0_gender_male Male
label var t0_gender_female Female
label var t0_race_white White
label var t0_race_afam "African American"
label var t0_educ_college_grad "College Grad"
label var t0_pid_democrat Democrat
label var t0_pid_indep_other "Independent or Other"
label var t0_pid_republican Republican
label var t0_ideo_very_liberal "Very Liberal"
label var t0_ideo_liberal Liberal
label var t0_ideo_moderate Moderate

label var t0_prior_campaign_experience "Prior Campaign Experience"
label var t0_prior_deepcanvasser "Prior Perspective-Getting Experience"

tabstat t0_ideo_very_liberal t0_ideo_liberal t0_ideo_moderate

eststo demos: quietly estpost summarize ///
    t0_age t0_gender_male t0_gender_female t0_race_white t0_race_afam ///
	t0_educ_college_grad t0_pid_democrat t0_pid_indep_other t0_pid_republican ///
	t0_ideo_very_liberal t0_ideo_liberal t0_ideo_moderate t0_prior_campaign_experience t0_prior_deepcanvasser

esttab demos using ../output/partisan_canvass_demos.tex, ///
	cells("mean(pattern(1 1 0) fmt(2)) sd(pattern(1 1 0))") ///
	label nonumbers title("Study 3 Summary Statistics" \label{partisancanvassdemos}) replace tex
 
eststo clear

foreach t in 0 1 {
	gen t`t'_trump_voter_diff = t`t'_therm_trump_voters - t`t'_therm_not_trump_voters
	gen t`t'_rep_voter_diff = t`t'_therm_rep_voters - t`t'_therm_dem_voters
	gen t`t'_rep_pols_diff = t`t'_therm_rep_pols - t`t'_therm_dem_pols
}

keep canvasser_id t*_therm_trump_voters t*_trump_voter_diff t*_therm_rep_voters t*_rep_voter_diff t*_therm_rep_pols t*_rep_pols_diff ///
	t*_therm_dem_voters t*_therm_dem_pols t*_therm_not_trump_voters t0_prior_deepcanvasser t1_open_end_coding

rename t1_* *1
rename t0_* *0

foreach var in therm_rep_voters therm_trump_voters therm_rep_pols {
	gen `var'_change = `var'1 - `var'0
}


// Paired t-test
ttest therm_trump_voters1 = therm_trump_voters0
ttest therm_rep_voters1 = therm_rep_voters0

// By open_end
tab open_end_coding1

outsheet open_end_coding1 therm_trump_voters1 therm_trump_voters0 therm_rep_voters1 therm_rep_voters0 using study3_with_open_ends.csv, comma replace
bysort open_end_coding1: ttest therm_trump_voters1 = therm_trump_voters0
bysort open_end_coding1: ttest therm_rep_voters1 = therm_rep_voters0

tabstat therm_rep_voters_change, by(open_end_coding1) s(mean N)
tabstat therm_trump_voters_change, by(open_end_coding1) s(mean N)
drop open_end_coding1
	
reshape long therm_rep_voters therm_rep_pols therm_trump_voters ///
	trump_voter_diff rep_voter_diff rep_pols_diff ///
	therm_dem_voters therm_dem_pols therm_not_trump_voters, i(canvasser_id) j(post) // id = unique identifier for each canvasser
	
foreach outcome of varlist therm_rep_voters therm_trump_voters therm_rep_pols ///
	rep_voter_diff trump_voter_diff rep_pols_diff ///
	therm_dem_voters therm_not_trump_voters therm_dem_pols { // Not in PAP, but a placebo test we can run: does this also increase feeling towards Dems?
		disp "`outcome'"
		areg `outcome' i.post, a(canvasser_id)
		estimates store `outcome'
}

esttab therm_rep_voters therm_trump_voters therm_rep_pols ///
	therm_dem_voters therm_not_trump_voters therm_dem_pols ///
	using ///
	../output/partisan_regression_results.tex, replace tex ///
	starlevels(* 0.10 ** 0.05 *** 0.01) se ///
	keep(1.post _cons) coeflabels(1.post "Change" _cons "Const.") ///
	title("Study 3 Regression Results") width(\hsize)  ///
	addnotes("All models include canvasser fixed effects") ///
	mtitle("R Voters" "Trump Voters" "R Pols." ///
	"D Voters" "Not Trump Voters" "D Pols." ///
	"R-D Voters" "Trump-Not Voters" "R-D Pols.") nonumbers

